<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('memory_limit', '1000M');
set_time_limit(0);
CONST TYPE_NORMAL = 1;
CONST TYPE_BAD = 2;
include_once('function.php');
include_once('sell.php');
include_once('rent.php');
$borough_file_name = 'borough';
$broker_check_file_name = 'broker_check';
$borough_info_file_name = 'borough_info';
$borough_price_file_name = 'borough_price';
$borough_file=file($borough_file_name);


function field_is_null($house_info){
    $house_info['created'] = time();
    $house_info['updated'] = time();
    if(($house_info['borough_name']=="" || empty($house_info['borough_name'])) && ($house_info['house_totalarea']=="" || empty($house_info['house_totalarea'])) && ($house_info['house_room']=="" || empty($house_info['house_room'])) && ($house_info['house_topfloor']=="" || empty($house_info['house_topfloor']))&& ($house_info['house_price']=="" || empty($house_info['house_price'])) && ($house_info['cityarea_id']=="" || empty($house_info['cityarea_id'])) && ($house_info['cityarea2_id']=="" || empty($house_info['cityarea2_id'])) && ($house_info['off_type']=="" || empty($house_info['off_type'])) ){
        $house_info['bad_type'] = 17;
    }
    return $house_info;
}
function hadleFields($house_info){
    //小区名不能为空，为空直接扔掉
    if(empty($house_info['borough_name']) || trimall($house_info['borough_name'])==""){
        $house_info['bad_type'] = 2;
        return $house_info;
        continue;
    }
    //房屋面积为空直接扔掉
    if(trimall($house_info['house_totalarea'])<0){
        $house_info['bad_type'] = 3;
        return $house_info;
        continue;
    }
    //卧室个数小于0扔掉
    if(trimall($house_info['house_room'])<0){
         $house_info['bad_type'] = 4;
         return $house_info;
         continue;
    }
    //总楼层小于0扔掉
    if(trimall($house_info['house_topfloor'])<0){
        $house_info['bad_type'] = 5;
        return $house_info;
        continue;
    }

    //出租价格小于等于0扔掉
    if(trimall($house_info['house_price'])<=0){
        $house_info['bad_type'] = 6;
        return $house_info;
        continue;
    }
    //厕所如果为空的话 默认值为1
    if(empty($house_info['house_toilet']) || $house_info['house_toilet']==""){
        $house_info['house_toilet']=1;
    }
    //对官网电话做统一处理
    if(strstr($house_info['owner_phone'],"-")){
        $house_info['owner_phone'] = str_replace("-","",$house_info['owner_phone']);
    }
    if(strstr($house_info['owner_phone'],"转")){
        $house_info['owner_phone'] = str_replace("转", ",",$house_info['owner_phone']);
    }
    return $house_info;
}
function hadleFieldsPersonal($house_info, $sell_config, $broker_check){
    //小区名不能为空，为空直接扔掉
    if((empty($house_info['borough_name']) || trimall($house_info['borough_name'])=="")&&(empty($house_info['cityarea_id']) || trimall($house_info['cityarea_id'])=="") && (empty($house_info['cityarea2_id']) || trimall($house_info['cityarea2_id'])=="") ){
        $house_info['bad_type'] = 16;
        return $house_info;
        
     }
    //检测标题关键字
    foreach($sell_config['ARRAY_TITLE_KW'] as $keyword){
        $result = strpos($house_info['house_title'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['bad_type'] = 11;
            return $house_info;
        }
    }
    //检测房源描述关键字
    foreach($sell_config['ARRAY_DESC_KW'] as $keyword){
        $result = strpos($house_info['house_desc'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['bad_type'] = 12;
            return $house_info;
        }
    }
    //检测房源标题是否含有经纪公司关键字
    foreach($sell_config['ARRAY_COMPANY_KW'] as $keyword){
        $result = strpos($house_info['house_title'],$keyword);

        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['bad_type'] = 13;
	    return $house_info;
        }
    }
    //检测房源描述是否含有经纪公司关键字
    foreach($sell_config['ARRAY_COMPANY_KW'] as $keyword){
        $result = strpos($house_info['house_desc'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['bad_type'] = 14;
	    return $house_info;
        }
    }
    //经纪人过滤
    if(!empty($broker_check[$house_info['owner_phone']])){
        $house_info['bad_type'] = 15;
        return $house_info;
    }
    return $house_info;
}
function outputBadHouse($house_info, $bad_type){
    $house_info['bad_type'] = $bad_type;
    $out = json_encode($house_info);
    echo $house_info['source_url']."\t".TYPE_BAD."\t".$out."\n";
}
function mergeOne($house_info, $rent_config){
    //电话为空的话 直接返回官网电话
    if($house_info['owner_phone']=="" || empty($house_info['owner_phone'])){
        if($house_info['source']!=10){
            if($house_info['service_phone'] == "" || empty($house_info['service_phone'])){
                $govtel = $rent_config['ARRAY_GOV_TEL'];
                $house_info['service_phone'] = $govtel[$house_info['source']];
            }
        }

     }
    //厅转换
    if($house_info['house_hall']){
        foreach ($rent_config['ARRAY_HALL_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_hall']))){
                $house_info['house_hall'] = $inkey;
                break;
            }
        }
    }
    //装修情况
    if($house_info['house_fitment']){
        foreach ($rent_config['ARRAY_FITMENT_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_fitment']))){
                $house_info['house_fitment'] = $inkey;
                break;
            }
        }
    }
    //朝向
    if($house_info['house_toward']){
        foreach ($rent_config['ARRAY_TOWARD_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_toward']))){
                $house_info['house_toward'] = $inkey;
                break;
            }
        }
    }
    //房源类型
    if($house_info['house_type']){
        foreach ($rent_config['ARRAY_HOUSETP_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_type']))){
                $house_info['house_type'] = $inkey;
                break;
            }
        }
    }
    //房屋楼层 高 中 低,判断是不是数字
    if($house_info['house_floor']){
        foreach ($rent_config['ARRAY_HFLOOR_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_floor']))){
                $house_info['house_floor'] = $inkey;
                break;
            }
        }
    }
    //tag 标签
    if(isset($house_info['tag'])){
        $house_info['tag'] = "";
        foreach ($rent_config['ARRAY_PAYM_TAG'] as $val=>$inkey){
            if(preg_match("/.*$val.*/",trimall($house_info['tag']))){
                $house_info['tag'] .= $inkey.'#';
            }
        }
    }
    //房源描述如果为空的话
    if(empty($house_info['house_desc']) || $house_info['house_desc']==""){
        $borough_info = $borough_info_tbl[$house_info['borough_id']];
        $borough_desc = [];
        if($borough_info){
            $borough_desc["附近有超市商场"] = $borough_info['borough_shop'];
            $borough_desc["附近有医院"] = $borough_info['borough_hospital'];
            $borough_desc["附近有学校"] = implode(" ",[$borough_info['nursery_school'],$borough_info['middle_school'],$borough_info['elementary_school']]);
            $borough_desc["附近交通也很便利"] = $borough_info['borough_bus'];
            $borough_desc["附近饭店也不错哦"] = $borough_info['borough_dining'];
        }
        if($house_info['source'] == 10){
            $house_desc = "我是业主，我的房子在".$house_info['borough_name'].",";
            foreach($borough_desc as $desc=>$desc_data){
                if(!empty($desc_data)){
                    $house_desc = $house_desc.$desc . ",";
                }
            }
            $house_desc .= "希望把房子交给有缘人。";
        }else{
            $sourcename = $rent_config["ARRAY_GOV_SOURCE"];
            $house_desc = "我是".$sourcename[$house_info['source']]."的一名经纪人，竭诚为您服务，这套房子在".$house_info['borough_name']."。";
            foreach($borough_desc as $desc=>$desc_data){
                if(!empty($desc_data)){
                    $house_desc = $house_desc.$desc . ",";
                }
            }
            $house_desc .= "我保证信息的真实，并期待您和我联系!";
        }
        $house_info['house_desc'] = $house_desc;
    }
    return $house_info;
}
$borough = array();
$borough_info = array();
$broker_check = array();
$borough_file = file($borough_file_name);
$borough_info_file = file($borough_info_file_name);
$broker_check_file = file($broker_check_file_name);
$borough_price_file = file($borough_price_file_name);
foreach($borough_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough[$line['borough_name']] = $line['id'];
}
$borough_info_keys = array('borough_dining', 'borough_shop', 'borough_bus', 'middle_school', 'elementary_school', 'nursery_school', 'borough_hospital');
foreach($borough_info_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough_info = [];
    foreach($borough_info_keys as $key)
    	$borough_info[$key] = $line[$key];
    $borough_info_tbl[$line['id']] = $borough_info;
}
foreach($broker_check_file as &$line_json){
    $line = json_decode($line_json, True);
    $broker_check[$line['phone']] = $line['id'];
}
foreach ($borough_price_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough_price[$line['borough_id']] = $line['price'];
}

#$gov_filename='house_sell_gov_test';
#$gov_file=file($gov_filename);
#foreach($gov_file as &$line_json){
while (($line_json = fgets(STDIN)) !== false) {
    $line_json = trim($line_json);
    if(empty($line_json))
        continue;
    $house_info = json_decode($line_json, true);
    $house_info = field_is_null($house_info);
    if(!empty($house_info['bad_type'])){
	outputBadHouse($house_info, $house_info['bad_type']);
	continue;
    }
    if($house_info['source']==10)
	$house_info = hadleFieldsPersonal($house_info, $sell_config, $broker_check);
    else
    	$house_info = hadleFields($house_info);
    if(!empty($house_info['bad_type'])){
        outputBadHouse($house_info, $house_info['bad_type']); 
	continue;
    }
    //处理小区
    if ($borough[$house_info['borough_name']])
	$house_info['borough_id'] = $borough[$house_info['borough_name']];	
    else{
	outputBadHouse($house_info, 19);
	continue;
    }
    if ($borough_price[$house_info['borough_id']]){
        $price=$borough_price[$house_info['borough_id']];
        if $house_info['house_price'] > $price*10 || $house_info['price'] < $price*0.1) {
	    outputBadHouse($house_info, 20);
            continue;
        }
        else if(($house_info['house_price'] <= $price*10 && $house_info['house_price'] > $price*2) || ($house_info['house_price'] < $price*0.5 && $house_info['house_price'] >= $price*0.1)){
            $house_info['tag'] = $house_info['tag'].'100#';
        }
    }
    //过滤Q房网
    if($house_info['source'] == 5){
	$res = preg_match('/Q房网/u',$v['company']);
        if(!$res){
	    outputBadHouse($house_info, 10);
            continue;
        }
    }
    $house_info = mergeOne($house_info, $rent_config);
    $out = json_encode($house_info);
    echo $house_info['source_url']."\t".TYPE_NORMAL."\t".$out."\n";
}
    

    
